import { AlignType, ClassName, CtrlType, Events, Shape } from "@/Constants";
import { IPixelPos, IPoint, IRelativePos, ISize } from "@/Interface";
import { Utils } from "../../utils";
import Feature from "../Feature";

// 基础点状元素
class Pnt extends Feature {

    type: CtrlType | AlignType | null = null;
    shape: Shape | Function = Shape.RECT  // 等于自定义Function时,就是自定义形状的意思
    // (path: Path2D, center: IPixelPos, width: number, height: number): Path2D {
    //     path.moveTo(center.x, center.y - height / 2);
    //     path.lineTo(center.x + width / 2, center.y + height / 2)
    //     path.lineTo(center.x - width / 2, center.y + height / 2);
    //     path.closePath();
    //     return path;
    // }
    position: IRelativePos = { x: 0, y: 0 }  // 元素包围盒的中心点, 一般Rect类型用
    size: ISize = { width: 0, height: 0 }  // 元素包围盒的宽高

    constructor(x: number = 0, y: number = 0, width = 10, height?:number) {
        super([{ x, y }]);
        this.size.width = width;
        if(height){
            this.size.height = height;
        }else {
            this.size.height = width;
        }
        this.className = ClassName.PNT
        this.fillStyle = this.focusStyle = "#85C1E9";
        this.hoverStyle = '#2980B9'
        this.strokeStyle = '#2980B9'
        this.isOnlyCenterAdsorb = true;
        this.zIndex = Infinity;
        this.lineWidth = 3;
        this.isClosePath = true;
        this.isPixelLen = true;
        this.cbDrawMiniFeature = true;
        Feature.Gls.addFeature(this);
    }

    override draw(ctx: CanvasRenderingContext2D, pointArr: IPixelPos[], lineWidth: number, lineDashArr: number[]) {
        this.setStyle(ctx, this.lineWidth);
        const center = Feature.getCenterPos(pointArr);
        const width = Feature.Gls.getRatioSize(this.size.width, this.isPixelLen)
        const height = Feature.Gls.getRatioSize(this.size.height, this.isPixelLen)
        let path = new Path2D();
        switch (this.shape) {
            case Shape.RECT:
                path.rect(center.x - width / 2, center.y - height / 2, width, height)
                break;
            case Shape.CIRCLE:
                path.arc(center.x, center.y, width / 2, 0, 360)
                break;
            default:
                path = this.getShape(path, center, width, height)
                break;
        }
        this.fillStyle && ctx.fill(path);
        if (this.isStroke && this.lineWidth > 0) ctx.stroke(path);
        this.setPointIn(ctx, Utils.getRectPoints(center, { width, height }), path);
    }

}

export default Pnt;